package cn.kgc.watch.web;

import cn.kgc.watch.entity.ResultInfo;
import cn.kgc.watch.entity.User;
import cn.kgc.watch.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
//登录
@CrossOrigin
public class loginController {
    @Resource
    private UserService userService;

    /**
     * 登录
     * @param user
     * @param session
     * @return
     */
    @PostMapping("login")
    public User login(@RequestBody User user, HttpSession session){
        User loginUser = this.userService.findUser(user);
        if(loginUser!=null){
            System.out.println("loginUser.getUserPassword() = " + loginUser.getUserPassword());
            this.userService.modifyLoginTime(loginUser.getUserId(),new Date());
            session.setAttribute("user",loginUser);
        }

        return loginUser;
    }
    /**
     * 查询用户名是否重复
     */
    @GetMapping("findUserName")
    public Integer findUserName(String userNameCheck){
        System.out.println("userNameCheck = " + userNameCheck);
        return this.userService.findUserName(userNameCheck);
    }

    /**
     * 注册
     * @param user
     */
    @PostMapping("register")
    public void register(@RequestBody User user){
        user.setCreateTime(new Date());
        this.userService.addUser(user);
    }


    /**
     * 查看原来的旧密码
     * */
    @GetMapping("showUserPassword")
    public ResultInfo showUserPassword(Integer userId,String password){
        return new ResultInfo(200,"成功",this.userService.findUserPassword(userId,password));
    }

    /**
     * 修改密码
     * */
    @PostMapping("updatePassword")
    public ResultInfo updatePassword(@RequestBody User user){
        this.userService.modifyPassword(user);
        return new ResultInfo(200,"成功");
    }
}
